package com.hqyj.springBoot.modules.account.dao;

import com.hqyj.springBoot.modules.common.vo.Search;
import com.hqyj.springBoot.modules.account.entity.Resource;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

@Mapper
@Repository
public interface ResourceDao {

    @Insert("insert into account_resource (create_date,permission) values (#{createDate},#{permission})")
    @Options(useGeneratedKeys = true,keyColumn = "id",keyProperty = "resourceId")
    void insertResource(Resource resource);

    @Select("select * from account_resource where permission = #{permission}")
    Resource selectResourceByPerMission(String permission);

    @Update("update account_resource set create_date = #{createDate} ,permission= #{permission} where id = #{resourceId}")
    void updateResource(Resource resource);

    @Delete("delete from account_resource where id = #{resourceId}")
    void deleteResourceById(int resourceId);

    @Select("select * from account_resource where id = #{resourceId}")
    @Results(id = "resourceResults",value = {
            @Result(column = "id",property = "resourceId"),
            @Result(column = "id",property = "roles",
                    many = @Many(select = "com.hqyj.springBoot.modules.account.dao.RoleDao.getRolesByResourceId"))
    })
    Resource selectResourceById(int resourceId);


    @Select("<script>" +
            "select * from account_resource " +
            "<where>" +
            "<if test='keyword != \"\" and keyword != null'>"+
            "and permission like '%${keyword}%'"+
            "</if>"+
            "</where>"+
            "<choose>"+
            "<when test='sort != \"\" and sort != null'>"+
            "order by ${sort} ${direction}"+
            "</when>"+
            "<otherwise>"+
            "order by id desc"+
            "</otherwise>"+
            "</choose>"+
            "</script>")
    @Results(id = "searchResults",value = {
            @Result(column = "id",property = "resourceId"),
            @Result(column = "id",property = "roles",
                    many = @Many(select = "com.hqyj.springBoot.modules.account.dao.RoleDao.getRolesByResourceId"))
    })
    List<Resource> getResourcesBySearch(Search search);
}
